package com.dearbinge.data.service.impl.user;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.user.UserBlackListService;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.dao.user.UserBlacklistMapper;
import com.dearbinge.data.pojo.im.vo.ImUserContactTagRelationVO;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.data.pojo.user.UserBlacklistDO;
import com.dearbinge.data.pojo.user.vo.UserBlacklistRequestVO;
import com.dearbinge.data.pojo.user.vo.UserBlacklistResponseVO;
import com.dearbinge.utils.Base64Util;
import com.dearbinge.utils.BeanMapperUtil;
import com.dearbinge.utils.PageUtils;
import com.dearbinge.utils.ResultMsg;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 黑名单实现层
 *
 * @author wes
 * @date 2019/4/8
 */
@Service(value = "userBlackListService")
public class UserBlackListImpl implements UserBlackListService {

    @Autowired
    private UserBlacklistMapper userBlacklistMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 删除
     *
     * @param blackId ID
     * @return 影响行数
     */
    @Override
    public int delete(Integer blackId) {
        return userBlacklistMapper.delete(blackId);
    }

    /**
     * 插入
     *
     * @param userBlacklistRequestVO VO
     * @return 影响行数
     */
    @Override
    public int insert(UserBlacklistRequestVO userBlacklistRequestVO) {
        UserBlacklistDO userBlacklist = userBlacklistMapper.getUserBlacklist(userBlacklistRequestVO.getMyId(),
                userBlacklistRequestVO.getBlackUserId());
        if (userBlacklist != null) {
            return 1;
        }
        String myId = userBlacklistRequestVO.getMyId().toString();
        UserBlacklistDO userBlacklistDO = new UserBlacklistDO();
        BeanUtils.copyProperties(userBlacklistRequestVO, userBlacklistDO);
        userBlacklistDO.setCreateBy(myId);
        userBlacklistDO.setModifyBy(myId);
        return userBlacklistMapper.insert(userBlacklistDO);
    }


    /**
     * 查询黑名单
     *
     * @param myId 我的用户id
     * @return 黑名单列表
     */
    @Override
    public Page listByMyId(Integer myId, Integer current, Integer size) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        Page page = new Page<ImUserContactTagRelationVO>(current, size);
        List<UserBlacklistDO> list = userBlacklistMapper.listByMyId(myId, page);
        List<UserBlacklistResponseVO> result = BeanMapperUtil.map(list, UserBlacklistResponseVO.class);
        for (UserBlacklistResponseVO userBlacklistResponseVO : result) {
            User user = userMapper.getById(userBlacklistResponseVO.getBlackUserId());
            String userName = user.getName();
            userBlacklistResponseVO.setUserName(Base64Util.decodeData(userName));
            userBlacklistResponseVO.setHeadImg(user.getHeadImg());
        }
        Integer count = userBlacklistMapper.countByMyId(myId);
        page.setTotal(count);
        page.setRecords(result);
        return page;
    }

    /**
     * 是否拉黑 0:未拉黑 blackId的值：已拉黑
     *
     * @param myId        我的用户id
     * @param blackUserId 被拉黑的用户id
     * @return blackId
     */
    @Override
    public ResultMsg<Integer> isBlack(Integer myId, Integer blackUserId) {
        UserBlacklistDO userBlacklist = userBlacklistMapper.getUserBlacklist(myId, blackUserId);
        UserBlacklistDO your = userBlacklistMapper.getUserBlacklist(blackUserId,myId );
        String msg;
        if (your != null || userBlacklist != null) {
            msg = "1";
        }else {
            msg = "0";
        }
        if (userBlacklist != null) {
            return ResultMsg.okData(userBlacklist.getBlackId(),msg);
        }
        return ResultMsg.okData(0,msg);
    }
}
